package com.yoda.youdianda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yoda.youdianda.common.R;
import com.yoda.youdianda.controller.SysConfigController;
import com.yoda.youdianda.domain.SysConfig;
import com.yoda.youdianda.dto.SysConfigDto;
import com.yoda.youdianda.mapper.SysConfigMapper;
import com.yoda.youdianda.service.SysConfigService;
import com.yoda.youdianda.vo.AllConfigVo;
import com.yoda.youdianda.vo.ChildrenConfigVo;
import com.yoda.youdianda.vo.MiniConfigVo;
import com.yoda.youdianda.vo.SettingsVo;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SysConfigServiceImpl implements SysConfigService {


    @Autowired
    private SysConfigMapper configMapper;


    @Override
    public AllConfigVo getAllConfig() {
        AllConfigVo vo = new AllConfigVo();
        List<SettingsVo> list = new ArrayList<>();
        //1 获取所有未删除设置
        LambdaQueryWrapper<SysConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysConfig::getDelFlag,0);
        List<SysConfig> sysConfigs = configMapper.selectList(wrapper);
        //2 获取所有的设置分类 预期:分类集合
        ArrayList<String> tabs = new ArrayList<>();
        ArrayList<String> groups = new ArrayList<>();

        List<MiniConfigVo> allData = new ArrayList<>();

        Map<String, String> formMap = new HashMap<>();//设置表里attr_key -> attr_value
        Map<String, String> rulesMap = new HashMap<>();//设置表里attr_key -> validator 不为空的字段映射
        for (SysConfig config : sysConfigs) {
            //参数不为空,且不包含则添加进入集合
            if (!Strings.isEmpty(config.getTabValue()) && !tabs.contains(config.getTabValue())) {
                tabs.add(config.getTabValue());
            }
            if (Strings.isNotEmpty(config.getGroupValue()) && !groups.contains(config.getGroupValue())) {
                groups.add(config.getGroupValue());
            }
            MiniConfigVo miniConfigVo = MiniConfigVo.init(config);
            allData.add(miniConfigVo);
            formMap.put(config.getAttrKey(), config.getAttrValue());

            if (Strings.isNotEmpty(config.getValidator())) {
                rulesMap.put(config.getAttrKey(), config.getValidator());
            }
        }

        //3 根据参数集合查找对应设置集合

        for (String tab : tabs) {
            List<ChildrenConfigVo> children = new ArrayList<>();
            //获取未细分的设置
            List<SysConfig> filter1 = sysConfigs.stream().filter(s -> tab.equals(s.getTabValue())).collect(Collectors.toList());
            //对设置按组进行细分
            for (String group : groups) {
                List<SysConfig> filter2 = filter1.stream().filter(s -> group.equals(s.getGroupValue())).collect(Collectors.toList());
                //转化vo
                if (filter2.size() > 0) {
                    ChildrenConfigVo childrenConfigVo = ChildrenConfigVo.init(group, filter2);
                    children.add(childrenConfigVo);
                }
            }
            SettingsVo settingsVo = SettingsVo.init(tab, children);
            list.add(settingsVo);
        }
        //vo  list部分完成
        vo.setList(list);
        vo.setAllData(allData);
        //vo all data完成
        vo.setConfigForm(formMap);
        vo.setConfigRules(rulesMap);

        return vo;

    }


    /**
     * 更新系统配置
     *
     * @param configDto 配置dto
     * @return boolean
     */
    @Transactional
    @Override
    public boolean updateSysConfig(List<SysConfigDto> configDto) {
        for (SysConfigDto sysConfigDto : configDto) {
            SysConfig sysConfig = SysConfigDto.init(sysConfigDto);
            configMapper.updateById(sysConfig);
        }
        return true;
    }
}